package com.pys.common.beans;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.component.accordionpanel.AccordionPanel;
import org.primefaces.context.RequestContext;
import org.primefaces.event.TabChangeEvent;

import com.google.gson.Gson;
import com.pys.menu.Menu;
import com.pys.menu.MenuParentItem;
import com.pys.utils.FacesUtils;

@SessionScoped
@ManagedBean
public class NavigationBean implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 *
	 */
	private List<MenuParentItem> parentList;
	private Menu menu;
	private Gson gson = new Gson();
	private String activeTabIndex;

	public String getActiveTabIndex() {
		return activeTabIndex;
	}

	public void setActiveTabIndex(String activeTabIndex) {
		this.activeTabIndex = activeTabIndex;
	}

	public void onTabChange(TabChangeEvent event) {
		String activeIndex = ((AccordionPanel) event.getComponent())
				.getActiveIndex();
		setActiveTabIndex(activeIndex);
	}

	// private MenuModel modelMenu;
	//
	// public MenuModel getModelMenu() {
	// return modelMenu;
	// }

	// private void initw() {
	// Logger.info(getClass(), "Navigation initted.");
	//
	// InputStream inputStr;
	// BufferedReader br = null;
	// String filePath = "";
	// gson = new Gson();
	// modelMenu = new DefaultMenuModel();
	//
	// try {
	// filePath = "/resources/menu/LeftNav_" +
	// FacesUtils.getUserSession().getLocale().toString() + ".json";
	//
	// inputStr = FacesUtils.getExternalContext().getResourceAsStream(filePath);
	// br = new BufferedReader(new InputStreamReader(inputStr, "UTF-8"));
	// // convert the json string back to object
	// this.menu = gson.fromJson(br, Menu.class);
	// } catch (Exception e) {
	// Logger.error(getClass(), e.getMessage());
	// } finally {
	// try {
	// br.close();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	// parentList = menu.getParentList();
	//
	// for (Iterator iterator = menu.getParentList().iterator();
	// iterator.hasNext();) {
	// MenuParentItem menuParentItem = (MenuParentItem) iterator.next();
	// Submenu menuParent = new Submenu();
	// menuParent.setLabel(menuParentItem.getTitle());
	// for (Iterator iterator1 = menuParentItem.getChildList().iterator();
	// iterator1.hasNext();) {
	// MenuChildItem menuChildItem = (MenuChildItem) iterator1.next();
	// MenuItem menuItem = new MenuItem();
	// menuItem.setValue(menuChildItem.getTitle());
	// menuItem.setUrl(menuChildItem.getLink());
	// menuItem.setTarget(menuChildItem.getType());
	// menuParent.getChildren().add(menuItem);
	// }
	// modelMenu.addSubmenu(menuParent);
	// }
	//
	// }

	@PreDestroy
	private void finit() {
		// Logger.info(getClass(), "Navigation finitted.");
		System.out.println("Navigation finitted.");
	}

	@PostConstruct
	private void init() {
		// Logger.info(getClass(), "Navigation initted.");
		System.out.println("Navigation initted.");

		InputStream inputStr;
		BufferedReader br = null;
		String filePath = "";
		gson = new Gson();
		try {
			filePath = "/resources/menu/left_nav_"
					+ FacesUtils.getUserSession().getLocale().toString()
					+ ".json";

			inputStr = FacesUtils.getFacesContext().getExternalContext()
					.getResourceAsStream(filePath);
			br = new BufferedReader(new InputStreamReader(inputStr, "UTF-8"));
			// convert the json string back to object
			this.menu = gson.fromJson(br, Menu.class);

		} catch (Exception e) {
			e.printStackTrace();
			// Logger.error(getClass(), e.getMessage());
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		parentList = menu.getParentList();
	}

	public List<MenuParentItem> getParentList() {
		return parentList;
	}

	public void setParentList(List<MenuParentItem> parentList) {
		this.parentList = parentList;
	}

	public void clear() {
		finit();
		// init();
	}

	public void viewCarsCustomized() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("modal", true);
		options.put("draggable", true);
		options.put("resizable", false);
		options.put("contentHeight", 700);
		options.put("contentWidth", 800);

		RequestContext.getCurrentInstance().openDialog("talep", options, null);
	}

	// public void navigateMainPageAction() {
	// FacesUtils.redirectToMainPage();
	// }
}
